#!/usr/bin/python

"""
for the non-integral part of fraction a/b, found the n-th occurence of digit m, where 0 <= m <= 9
2013 Yahoo competition question 1
"""


def getNthPosViolent(a, b, m, n):
    import decimal
    precision = 2000
    decimal.getcontext().prec = precision
    a %= b
    q = decimal.Decimal(a)/decimal.Decimal(b)
    strRep = str(q)
    pointPos = strRep.find('.')
    pos = 1
    nFound = 0
    while True: 
        if int(strRep[pointPos+pos]) == m:
            nFound += 1
            if nFound == n:
                break
        pos += 1
        if pos >= precision:
            break
    
    if nFound == n:
        print pos
    else:
        print 0


def fracDecompose(a, b):
    a %= b
    divdend = a*10
    remainderList = [a]
    qDigitList = [0]
    pos = 1
    RecurStart = -1
    RecurEnd = -1
    while True:
        qDigit = divdend / b
        remainder = divdend % b
        qDigitList.append(qDigit)
        if remainder == 0:
            break
        if remainder in remainderList:
            RecurStart = remainderList.index(remainder) + 1
            RecurEnd = pos+1
            break
        remainderList.append(remainder)
        divdend = remainder * 10
        pos += 1

    if RecurStart == -1:
        return qDigitList[1:], []
    else:
        return qDigitList[1:RecurStart], qDigitList[RecurStart:RecurEnd]


def listNIndex(aList, aElem, nCount):
    aPos = 0
    assert aList.count(aElem) >= nCount
    for elem in aList:
        if aElem == elem:
            nCount -= 1
            if nCount == 0: 
                return aPos
        aPos += 1


def getNthPos(a, b, m, n):
    prefix, repetend = fracDecompose(a, b)
    if prefix.count(m) >= n:
        return 1 + listNIndex(prefix, m, n)
    else:
        if m not in repetend:
            return 0
        else:
            countLeft = n - prefix.count(m)
            if countLeft % repetend.count(m):
                nRepetend = countLeft/repetend.count(m) #number of complete repetends to go
            else:
                nRepetend = countLeft/repetend.count(m)-1 #number of complete repetends to go
            countLeft -= nRepetend*repetend.count(m)
            return 1 + len(prefix) + nRepetend*len(repetend) + listNIndex(repetend, m, countLeft)



if __name__ == '__main__':
    #TO RUN: cat input01.txt | python frac2Digits.py 
    import sys
    firstLine = sys.stdin.readline()
    words = firstLine.split()
    ntestcase = int(words[0])

    for lineno in range(0, ntestcase):
        line = sys.stdin.readline()
        words = line.split()
        a, b, n, m = int(words[0]), int(words[1]),int(words[2]),int(words[3])
        print getNthPos(a, b, m, n)
        #getNthPosViolent(a, b, m, n)




